package com.grf.hadoop.temp_grouping;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class TempBean implements WritableComparable<TempBean> {

    private String year;
    private double temp;

    public TempBean() {
        super();
    }

    public TempBean(String year, double temp) {
        this.year = year;
        this.temp = temp;
    }

    @Override
    public int compareTo(TempBean o) {
        int res;

        if (year.substring(0,4).compareTo(o.getYear().substring(0,4)) > 0) {
            res = 1;
        } else if (year.substring(0,4).compareTo(o.getYear().substring(0,4)) < 0) {
            res = -1;
        } else {
            if (temp > o.getTemp()) {
                res = -1;
            } else if (temp < o.getTemp()) {
                res = 1;
            } else {
                res = 0;
            }
        }

        return res;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(year);
        dataOutput.writeDouble(temp);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        year = dataInput.readUTF();
        temp = dataInput.readDouble();
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public double getTemp() {
        return temp;
    }

    public void setTemp(double temp) {
        this.temp = temp;
    }

    @Override
    public String toString() {
        return year + " " + temp;
    }
}
